//
//  AccountViewController.m
//  ZYTS-iPhone
//
//  Created by yuzhou on 12-12-7.
//  Copyright (c) 2012年 wzyk. All rights reserved.
//

#import "AccountViewController.h"
#import "LoginViewController.h"
#import "UserInfoViewController.h"

@interface AccountViewController ()

@end

@implementation AccountViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
        self.navigationBarHidden = YES;
        
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(loginSuccess) name:kLoginSuccess object:nil];
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(loginFail) name:kLoginFail object:nil];
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(registerSuccess) name:kRegisterSuccess object:nil];
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(registerFail) name:kRegisterFail object:nil];
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(updateDisplay) name:kLogout object:nil];
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(updateDisplay) name:UIApplicationDidBecomeActiveNotification object:nil];
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    [self updateDisplay];
}

- (void)updateDisplay
{
//    if (_needLogin) {
//        self.view.frame = CGRectMake(0, 0, 320, iPhone5 ? 548 : 460);
//    }
//    else {
//        self.view.frame = CGRectMake(0, 0, 320, iPhone5 ? 548 : 460);
//    }
    if ([[AuthorManager defaultManager] hasLoggedIn]) {
        UserInfoViewController *userInfoViewController = [[[UserInfoViewController alloc] initWithNibName:@"UserInfoViewController" bundle:nil] autorelease];
        [self setViewControllers:[NSArray arrayWithObject:userInfoViewController]];
    }
    else {
        LoginViewController *loginViewController = [[[LoginViewController alloc] initWithNibName:@"LoginViewController" bundle:nil] autorelease];
        loginViewController.needLogin = _needLogin;
        loginViewController.wantsFullScreenLayout = YES;
        [self setViewControllers:[NSArray arrayWithObject:loginViewController]];
    }
}

- (void)loginSuccess
{
    ABPromptView *promptView = [[[ABPromptView alloc] initWithTitle:@"登录成功"] autorelease];
    [promptView show];
    if (_needLogin) {
        [self.view.window.rootViewController dismissViewControllerAnimated:YES completion:nil];
    }
    else {
        [self updateDisplay];
    }
}

- (void)loginFail
{
    ABPromptView *promptView = [[[ABPromptView alloc] initWithTitle:@"用户名或密码错误"] autorelease];
    [promptView show];
}

- (void)registerSuccess
{
    [self loginSuccess];
}

- (void)registerFail
{
    ABPromptView *promptView = [[[ABPromptView alloc] initWithTitle:@"注册失败，请重试"] autorelease];
    [promptView show];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
    [super dealloc];
}
@end
